using System;
using System.IO;
using System.Xml;

namespace FreeFlow.Client
{
  /// <summary>The language used for client scripts.</summary>
  public enum ClientScriptLanguage
  {
    /// <summary>JScript language</summary>
    JScript,
    /// <summary>VBScript language</summary>
    VBScript
  }

	/// <summary>
	/// A client-side script.
	/// </summary>
	[Serializable]
	public class ClientScript
	{
    private AttachmentType attachmentType;
    private IGetAttachment getAttachment;
    /// <summary>
    /// Creates a new <see cref="ClientScript"/> instance.
    /// </summary>
    /// <param name="scriptNode">Script XML node.</param>
    /// <param name="getAttachment">The IGetAttachment implementation used to get attachments.</param>
    internal ClientScript(XmlElement scriptNode, IGetAttachment getAttachment)
    {
      this.getAttachment = getAttachment;
      if (scriptNode != null)
      {
        name = scriptNode.GetAttribute("Name");
        language = ParseLanguage(scriptNode.GetAttribute("Language"));

        // attachment type
        string type = scriptNode.GetAttribute("Type");
        if (type == "Procedure")
          attachmentType = AttachmentType.Procedure;
        else if (type == "Service")
          attachmentType = AttachmentType.Service;
        else 
          throw new FreeFlowException("Unrecognized attachment type - " + type);
      }
    }

    /// <summary>
    /// Parses the language attribute of a script element.
    /// </summary>
    /// <param name="language">Language.</param>
    public static ClientScriptLanguage ParseLanguage(string language)
    {
      if (string.Compare(language, "JScript", true) == 0)
        return ClientScriptLanguage.JScript;
      else if (string.Compare(language, "VBScript", true) == 0)
        return ClientScriptLanguage.VBScript;
      else
        throw new FreeFlowException("Unrecognized language - " + language);
    }

    #region properties
    private string name;
    /// <summary>
    /// Gets the name of the client script.
    /// </summary>
    public string Name
    {
      get
      {
        return name;
      }
    }

    private ClientScriptLanguage language;
    /// <summary>
    /// Gets the language of the client script call.
    /// </summary>
    public ClientScriptLanguage Language
    {
      get
      {
        return language;
      }
    }

    private string script;
    /// <summary>
    /// Gets the client script.
    /// </summary>
    public string Script
    {
      get
      {
        // get the script from the engine
        if (script == null)
        {
          // TODO - get the owner of the script (procedure name)
          byte[] data = getAttachment.GetAttachment(attachmentType, name, "");
          StreamReader reader = new StreamReader(new MemoryStream(data));
          script = reader.ReadToEnd();
        }
        return script;
      }
    }
    #endregion
	}
}
